


#include "VectorDataset.h"
#include <cmath>

VectorDataset::VectorDataset()
{
}

VectorDataset::VectorDataset(double* x, double* y, int dim)
{
    d_x = x;
    d_y = y;
    d_dim = dim;
}

Vector2f VectorDataset::getData(int idx)
{
    return Vector2f(d_x[idx], d_y[idx]);
}

Vector2f VectorDataset::getData(float x, float y)
{
    int idx1 = floor(y) * d_dim + floor(x);
    int idx2 = floor(y) * d_dim + floor(x + 1);
    int idx3 = floor(y + 1) * d_dim + floor(x);
    int idx4 = floor(y + 1) * d_dim + floor(x + 1);
    
    if (idx3 >= d_dim * d_dim)
        idx3 = idx1;
    
    if (idx4 >= d_dim * d_dim)
        idx4 = idx2;
    
    Vector2f v1 = getData(idx1);
    Vector2f v2 = getData(idx2);
    Vector2f v3 = getData(idx3);
    Vector2f v4 = getData(idx4);
    
    float dum;
    float r = 1 - std::modf(x, &dum);
    float s = 1 - std::modf(y, &dum);
    
    Vector2f leftMean = v1 * s + v3 * (1 - s);    
    Vector2f rightMean = v2 * s + v4 * (1 - s);
    
    return leftMean * r + rightMean * (1 - r);
}
        